<?php

namespace app\common\logic;

use app\common\model\User as User;
use app\common\model\Role as Role;
use app\common\model\Group as Group;
use app\common\model\UserOrg as UserOrg;
use app\common\model\Hospital as Hospital;
use app\common\model\Company as Company;
use app\common\model\File as File;
use app\common\model\GroupRolePrivilege as GroupRolePrivilege;
use app\common\model\UserPrivilege as UserPrivilege;
use app\common\model\Privilege as Privilege;
use app\common\model\UserLoginLog as UserLoginLog;

class UserLogic {

    //添加非认证用户
    public function addNoAuth($data) {
        $user = new User;

        $data['created_at'] = date('Y-m-d H:i:s', time());
        // $data['updated_at'] = date('Y-m-d H:i:s', time());

        $user->data($data);
        if ($user->allowField(true)->save()) {
            $uid = $user->id;
            $data2 = [
                'user_id' => $uid,
                'group_id' => $data['group_id'],
                'role_id' => $data['role_id']
            ];
            $userOrg = new UserOrg;
            $userOrg->data($data2);
            if ($userOrg->save()) {
                return $uid;
            }
        };
        return NULL;
    }

    //
    public function chkU($one) {
        $res = db(config('dbx.SU'))->name('SystemUser')->where('username', $one)
                ->whereOr('phone', $one)
                ->field('username,status,app_auth')
                ->find();
        if($res['authorize']){
            $res['app_auth'] = db('SystemAuth')->where(['id' => ['in', $res['authorize']]])->value('app_auth');
        }
        return $res;
    }

    //添加后台用户
    public function addAdmin($data) {
        
    }

    //认证用户
    public function upAuth($data) {
        
    }

    public function addNoAuth2($data) {
        $user = new User;

        $data['created_at'] = date('Y-m-d H:i:s', time());
        // $data['updated_at'] = date('Y-m-d H:i:s', time());

        $user->data($data);
        if ($user->allowField(true)->save()) {
            return $user;
        };
        return NULL;
    }

    //检测用户的唯一性
    public function checkUserHas($onlyOne) {
        $user = new User;
        $result = $user->where('name', $onlyOne)
                ->whereOr('phone', $onlyOne)
                ->find();
        if ($result) {
            $uid = $result->id;
            $userOrg = UserOrg::where('user_id', $uid)->find();
            if ($userOrg) {
                $result->userOrg = $userOrg;
            }
        }
        return $result;
    }

    public function auth($name, $pw) {
        $user = new User;
        $result = $user->where('name|phone', $name)
                ->where('password', $pw)
                ->find();
        if ($result) {
            $uid = $result->id;
            $userOrg = UserOrg::where('user_id', $uid)->find();

            $result->userOrg = $userOrg;
            if ($userOrg) {
                if ($userOrg->role_id == 1 && $userOrg->group_id == 1) {
                    // var_dump(1111);
                    $result->hospital = Hospital::get($userOrg->org_id);
                    if ($result->hospital) {
                        if ($result->hospital->file1_id) {
                            $result->hospital->file1 = File::get($result->hospital->file1_id);
                        }
                    }
                } else if ($userOrg->role_id == 1 && $userOrg->group_id == 3) {
                    $result->company = Company::get($userOrg->org_id);
                    if ($result->company) {
                        if ($result->company->file3_id) {
                            $result->company->file3 = File::get($result->company->file3_id);
                        }
                        if ($result->company->file4_id) {
                            $result->company->file4 = File::get($result->company->file4_id);
                        }
                    }
                }
            }
        }
        return $result;
    }

    public function checkUserOrg($uid) {
        $userOrg = UserOrg::where('user_id', $uid)->find();
        return $userOrg;
    }

    public function getOne($uid) {
        $user = new User;
        $result = $user->get($uid);
        if ($result) {
            $userOrg = UserOrg::where('user_id', $uid)->find();

            $result->userOrg = $userOrg;
            if ($userOrg) {
                if ($userOrg->role_id == 1 && $userOrg->group_id == 1) {
                    $result->hospital = Hospital::get($userOrg->org_id);
                    if ($result->hospital) {
                        if ($result->hospital->file1_id) {
                            $result->hospital->file1 = File::get($result->hospital->file1_id);
                        }
                    }
                } else if ($userOrg->role_id == 1 && $userOrg->group_id == 3) {
                    $result->company = Company::get($userOrg->org_id);
                    if ($result->company) {
                        if ($result->company->file3_id) {
                            $result->company->file3 = File::get($result->company->file3_id);
                        }
                        if ($result->company->file4_id) {
                            $result->company->file4 = File::get($result->company->file4_id);
                        }
                    }
                }
            }

            //权限获取
            $user_privileges = UserPrivilege::where('user_id', $uid)->find();
            $user_privilege = new UserPrivilege();
            $group_role_privilege = new GroupRolePrivilege();
            $privilege = new Privilege();
            $privs = [];
            if($user_privileges){
              $gr_privilege_ids = $user_privilege->getValue(['user_id' => $uid],'gr_privilege_ids');//
              $pre_privilege_ids = $group_role_privilege->getColumn(['id' => ['in', $gr_privilege_ids]],'privilege_ids');//
              $privilege_ids = '';
              $vv = [];
              foreach ($pre_privilege_ids as $k => $v) {
                if($v){
                  $vv[$k] = $v;
                }
              }
              $privilege_ids = implode(',', $vv);
              $privs = $privilege->getColumn(['id' => ['in', $privilege_ids]],'id,url');//具体权限
              // pp($privilege_ids);
            }
            $result->privs = $privs;
        }

        return $result;
    }

    public function bindOne($data) {
        $user = new User;
        $result = $user->allowField(true)->save($data, ['id' => $data['id']]);
        return $result;
    }

    //登录日志
    public function exeLog($data) {
        $userloginlog = new UserLoginLog;
        $result = $userloginlog->allowField(true)->save($data);
        return $result;
    }

    public function bindOneOrg($data) {
        $org = new UserOrg;
        $result = $org->where('user_id', $data['user_id'])
                ->find();
        
        if($result) {
            $result->data([
                'user_id' => $data['user_id'],
                'group_id' => $data['group_id'],
                'role_id' => $data['role_id']
            ]);
            $result->user_id = $data['user_id'];
            $result->group_id = $data['group_id'];
            $result->role_id = $data['role_id'];
            return $result->save();
        } else {
            $org->data([
                'user_id' => $data['user_id'],
                'group_id' => $data['group_id'],
                'role_id' => $data['role_id']
            ]);
            return $org->allowField(true)->save();
        }
    }
    
    public function upOneOrg($uid, $orgid) {
        $org = new UserOrg;
        $result = $org->where('user_id', $uid)
                ->find();
        if($result) {
            $result->org_id = $orgid;
            return $result->save();
        }
        return $result;
    }

    public function checkUserHas2($name, $mobile) {
        $user = new User;
        $result = $user->where('name', $name)
                ->where('phone', $mobile)
                ->find();
        return $result;
    }

    public function changePw($uid, $pw) {
        $user = new User;
        $result = $user->get($uid);
        $result->password = $pw;
        return $result->save();
    }

}
